# coding=utf8
from .models import ZoneInfo


def getZoneCodesByCode(env_name, zone_code = None):
    _list = []
    if zone_code is None:
        return _list
    _list.append(zone_code)
    for zone in ZoneInfo.query.filter_by(env_name = env_name, parent_code = zone_code).all():
        _list.append(zone.zone_code)
    return _list


def getZoneCodesByCodeS(env_name, zone_codes = None):
    _list = []
    if zone_codes is None:
        return _list

    _parents = []
    for zone in ZoneInfo.query.filter(ZoneInfo.zone_code.in_(str(zone_codes).split(','))).filter_by(
            env_name = env_name).all():
        if zone.parent_code is not None and zone.parent_code not in _parents:
            _parents.append(zone.parent_code)

    for _code in str(zone_codes).split(','):
        if _code not in _list and _code not in _parents:
            _list.append(_code)
            for zone in ZoneInfo.query.filter_by(env_name = env_name, parent_code = _code).all():
                if zone.zone_code not in _list:
                    _list.append(zone.zone_code)
    return _list
